package com.gin.no_gay_alliance.viewmodel;

import com.gin.common.utils.TimeUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.field.LabelValue;
import com.gin.nga.response.field.mission.MissionInfo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 任务信息
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/13 11:03
 **/
public class MissionModel {
    public Integer id;

    public String name;
    public List<LabelValue> props = new ArrayList<>();
    /**
     * 是否可签到
     */
    public Boolean checkAble = null;

    public MissionModel() {
    }

    public MissionModel(MissionInfo item) {
        this.id = item.getId();
        this.name = item.getName();
        if (!ObjectUtils.isEmpty(item.getInfo())) {
            props.add(new LabelValue("任务信息", item.getInfo()));
        }
        if (!ObjectUtils.isEmpty(item.getDetail())) {
            props.add(new LabelValue("任务详情", clearText(item.getDetail())));
        }
        if (!ObjectUtils.isEmpty(item.getStatus())) {
            props.add(new LabelValue("任务状态", clearText(item.getStatus())));
        }
        if (item.getMissionStatus() != null) {
            ZonedDateTime lastCountTime = item.getMissionStatus().getLastCountTime();
            lastCountTime = lastCountTime != null ? lastCountTime : ZonedDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneId.systemDefault());
            props.add(new LabelValue("上次完成", TimeUtils.format(lastCountTime, TimeUtils.SHORT_FORMATTER)));
        }
        checkAble = item.isCheckAble();
    }

    /**
     * 删除文本最后多余的换行
     *
     * @param s 文本
     * @return 文本
     */
    private static String clearText(String s) {
        if (s.endsWith("\n\n")) {
            return s.substring(0, s.length() - 2);
        }
        return s;
    }
}
